﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yichu_front
{
    public partial class Form产品价格查询 : Form
    {
        public Action<string, string> 选择产品;
        public Form产品价格查询()
        {
            InitializeComponent();
        }

        private void button查询_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = 查询();
            this.dataGridView1.Refresh();
        }

        private DataTable 查询()
        {
            string sql = @"select BOMType.型号类别,BOMModel.型号, ROW_NUMBER() OVER (ORDER BY 编码) 序号, BOMList.规格, 单价,  编码, 导体折数, 外径下限, 标称外径, 外径上限, 额定电压, 导体电阻, 耐压等级, 近似重量
                        from BOMModel join 
                         BOMType on BOMModel.bomtypeId = BOMType.id
                         join BOMList on BOMList.bomModelId = BOMModel.id
                         where 1=1";
            if (string.IsNullOrEmpty(textBox编码.Text.Trim()) == false)
            {
                sql += $" and BOMList.编码 like '%{textBox编码.Text.Trim()}%'";
            }
            if (string.IsNullOrEmpty(textBox分类.Text.Trim()) == false)
            {
                sql += $" and BOMType.型号类别 like '%{textBox分类.Text.Trim()}%'";
            }
            if (string.IsNullOrEmpty(textBox型号.Text.Trim()) == false)
            {
                sql += $" and BOMModel.型号 like '%{textBox型号.Text.Trim()}%'";
            }
            if (string.IsNullOrEmpty(textBox规格.Text.Trim()) == false)
            {
                sql += $"and BOMList.规格 like '%{textBox规格.Text.Trim()}%'";
            }
            sql += $" and BOMList.创建时间 >= '{dateTimePicker开始.Value.ToString("yyyy-MM-dd")}' and BOMList.创建时间 < '{dateTimePicker结束.Value.AddDays(1).ToString("yyyy-MM-dd")}'";
            sql += "order by BOMList.修改时间";
            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            return dt;

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string 型号 = dataGridView1.Rows[e.RowIndex].Cells["型号"].Value.ToString();
                string 编码 = dataGridView1.Rows[e.RowIndex].Cells["编码"].Value.ToString();
                选择产品(型号, 编码);
            }
        }

        private void Form产品价格查询_Load(object sender, EventArgs e)
        {
            //去年1月1
            dateTimePicker开始.Value = new DateTime(2023,1,1);
        }
    }
}
